{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 1. for_in循环使用"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "P\n",
      "y\n",
      "t\n",
      "h\n",
      "o\n",
      "n\n"
     ]
    }
   ],
   "source": [
    "# for-in遍历，对象必须是可迭代对象（字符串、序列等）\n",
    "for item in 'Python':  # 第一次取出来的是P，将P赋值item，将item的值输出\n",
    "    print(item)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "1\n",
      "2\n",
      "3\n",
      "4\n",
      "5\n",
      "6\n",
      "7\n",
      "8\n",
      "9\n"
     ]
    }
   ],
   "source": [
    "# range()产生一个整数序列，也是一个可迭代对象\n",
    "for i in range(10):\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "人生苦短，我用Python\n",
      "人生苦短，我用Python\n",
      "人生苦短，我用Python\n",
      "人生苦短，我用Python\n",
      "人生苦短，我用Python\n"
     ]
    }
   ],
   "source": [
    "# 如果在循环体中不需要使用到自定义变量，可将自定义变量写为“_”\n",
    "for _ in range(5):\n",
    "    print('人生苦短，我用Python')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1到100的偶数和为： 2550\n"
     ]
    }
   ],
   "source": [
    "# 使用for循环计算1到100的偶数\n",
    "sum1 = 0\n",
    "for item in range(1, 101):\n",
    "    if item % 2 == 0:\n",
    "        sum1 += item\n",
    "print('1到100的偶数和为：', sum1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "153\n",
      "370\n",
      "371\n",
      "407\n"
     ]
    }
   ],
   "source": [
    "# 使用for循环计算100到999之间的水仙花数（三位数，各个位数的三次方之和等于本身，153）\n",
    "for item in range(100,1000):\n",
    "    ge = item % 10\n",
    "    shi = item // 10 % 10\n",
    "    bai = item // 100\n",
    "    if ge ** 3 + bai ** 3 + shi ** 3 == item:\n",
    "        print(item)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.6.3",
   "language": "python",
   "name": "python3.6.3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": false,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
